import Mock from 'mockjs'
import apiUrl from '@/apis/urls/article'

const List = []
const count = 100

const baseContent = '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>'
const image_uri = 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'

for (let i = 0; i < count; i++) {
    List.push(Mock.mock({
        id: '@increment',
        timestamp: +Mock.Random.date('T'),
        author: '@first',
        reviewer: '@first',
        title: '@title(5, 10)',
        content_short: 'mock data',
        content: baseContent,
        forecast: '@float(0, 100, 2, 2)',
        importance: '@integer(1, 3)',
        'type|1': ['CN', 'US', 'JP', 'EU'],
        'status|1': ['published', 'draft'],
        display_time: '@datetime',
        comment_disabled: true,
        pageviews: '@integer(300, 5000)',
        image_uri,
        platforms: ['a-platform']
    }))
}

const fetchList = {
    type: 'get',
    url: apiUrl.fetchList,
    response: function (req) {
        const { importance, type, title, page = 1, limit = 20, sort } = req || {}
        let mockList = List.filter(item => {
            if (importance && item.importance !== +importance) return false
            if (type && item.type !== type) return false
            if (title && item.title.indexOf(title) < 0) return false
            return true
        })
        if (sort === '-id') {
            mockList = mockList.reverse()
        }
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
        return {
            total: mockList.length,
            items: pageList
        }
    },
}

const fetchArticle = {
    type: 'get',
    url: apiUrl.fetchArticle,
    response: function (req) {
        const id = req.query
        for (const article of List) {
            if (article.id === +id) {
                return article
            }
        }
    }
}

const fetchPv = {
    type: 'get',
    url: apiUrl.fetchPv,
    response: () => {
        return {
            pvData: [
                { key: 'PC', pv: 1024 },
                { key: 'mobile', pv: 1024 },
                { key: 'ios', pv: 1024 },
                { key: 'android', pv: 1024 }
            ]
        }
    }
}

const createArticle = {
    type: 'post',
    url: apiUrl.createArticle,
    response: 'success'
}

const updateArticle = {
    type: 'post',
    url: apiUrl.updateArticle,
    response: 'success'
}


export default [
    fetchList,
    fetchArticle,
    fetchPv,
    createArticle,
    updateArticle,
]